﻿///
///报表公用JS，依赖JQuery
///
var report = function (options) {
    var settings = {
        ddlHtdId: "", //标段下拉框id
        ddlMeasureId: "", //计量期下拉框id
        btnExportId: "", //导出报表按钮id
        webOfficeId: "", //webOffice的id
        measureDataUrl: "", //获取计量期数据的url
        exportDataUrl: "", //获取导出数据的url
        reportDataUrl: ""//获取报表数据的url
    };
    if (!!options) {
        $.extend(settings, options);
    }
    //
    //初始化
    //
    this.init = function () {
        _setContentHeight();
        _bindHtdChange();
        _bindMeasureChange();
        _setDefaultSelect();
    }

    //
    //计算分栏高度
    //
    function _setContentHeight() {
        $("#" + settings.btnExportId).hide(); //先隐藏导出报表按钮
        var contentHeight = $("#tabContent").innerHeight() - $(".panelBar").height();
        $("#" + settings.ddlHtdId).closest("div").next("div").height(contentHeight);

        //        $('#mainContent_ChangeListReport').layout({
        //            resizerClass: 'ui-state-default',
        //            west__onresize: function (pane, $Pane) {
        //            }
        //        });
    }
    //
    //默认选中标段
    //
    function _setDefaultSelect() {
        //默认选中最后一次选择的标段，如果没有就选择第一个标段
        if (!!window.lastSelectHtd && window.lastSelectHtd > 0 && window.lastSelectHtd < $("#" + settings.ddlHtdId).find("option").size()) {
            $("#" + settings.ddlHtdId).get(0).selectedIndex = lastSelectHtd;
        }
        else {
            $("#" + settings.ddlHtdId).get(0).selectedIndex = 1;
        }
        $("#" + settings.ddlHtdId).change();
    }
    //
    //绑定标段下拉框change
    //
    function _bindHtdChange() {
        $("#" + settings.ddlHtdId).change(function () {
            lastSelectHtd = $("#" + settings.ddlHtdId).get(0).selectedIndex; //记录用户选择的标段 ThinkWang 2012-11-1 
            var htd = $(this).val();
            if (htd == "" || htd == "0") {
                $("#" + settings.ddlMeasureId).find("option").remove();
                $("#" + settings.ddlMeasureId).append("<option value='0'>--请选择计量期--</option>");

                //默认选中最后一个计量期
                $("#" + settings.ddlMeasureId).get(0).selectedIndex = $("#" + settings.ddlMeasureId).find("option").size() - 1;
                $("#" + settings.ddlMeasureId).change();
            }
            else {
                $.post(settings.measureDataUrl + htd,
                {},
                 function (json) {
                     var optHtml = "";
                     if (json != null) {
                         if (json.statusCode == 200) {
                             var _id = "";
                             var _code = "";
                             for (var i = 0; i < json.data.length; i++) {
                                 if (!!json.callbackType && json.callbackType == "JL") {
                                     _id = json.data[i].VISIONCAL_ID;
                                     _code = json.data[i].VISIONCAL_CODE;
                                 }
                                 else {
                                     _id = json.data[i].SESSION_ID;
                                     _code = json.data[i].SESSION_CODE;
                                 }
                                 optHtml += "<option value='" + _id + "' >" + _code + "</option>";
                             }
                         }
                         else {
                             alertMsg.info(json.message);
                         }
                     }
                     if ($(optHtml).filter(function () { return $(this).val() == 0; }).size() < 1)
                         optHtml = "<option value='0'>--请选择计量期--</option>" + optHtml;

                     $("#" + settings.ddlMeasureId).find("option").remove();
                     $("#" + settings.ddlMeasureId).append(optHtml);

                     //默认选中上次选择的计量期，如果没有就选择最后一个计量期
                     if (!!window.lastSelectMeasure && window.lastSelectMeasure > 0 && window.lastSelectMeasure < $("#" + settings.ddlMeasureId).find("option").size()) {
                         $("#" + settings.ddlMeasureId).get(0).selectedIndex = window.lastSelectMeasure;
                     }
                     else {
                         $("#" + settings.ddlMeasureId).get(0).selectedIndex = $("#" + settings.ddlMeasureId).find("option").size() - 1;
                     }
                     $("#" + settings.ddlMeasureId).change();
                 },
                 "json");
            }
        });
    }
    //
    //绑定计量期下拉框change
    //
    function _bindMeasureChange() {
        $("#" + settings.ddlMeasureId).change(function () {
            window.lastSelectMeasure = $("#" + settings.ddlMeasureId).get(0).selectedIndex; //记录用户选择的计量期 ThinkWang 2012-11-1 

            var measureVal = $("#" + settings.ddlMeasureId).val();
            var segVal = $("#" + settings.ddlHtdId).val();

            if (measureVal != "" && measureVal != "0") {
                //导出按钮
                $("#" + settings.btnExportId).attr("href", settings.exportDataUrl + "?sessionId=" + measureVal + "&htdId=" + segVal);
                $("#" + settings.btnExportId).show();
            }
            else {
                $("#" + settings.btnExportId).hide();
            }
            _setReportData(measureVal, segVal);

        });
    }
    //
    //设置报表数据
    //
    function _setReportData(measureVal, segVal) {
        //if (!_checkWebOffice(settings.webOfficeId)) {
        //    return;
        //}
        var objWebOffice = $("#" + settings.webOfficeId).get(0);
        if (measureVal != "" && measureVal != "0") {
            PDFObject.embed(settings.reportDataUrl + "?sessionId=" + measureVal + "&htdId=" + segVal + "&" + new Date().getMilliseconds(), "#" + settings.webOfficeId);
            //            objWebOffice.LoadOriginalFile(settings.reportDataUrl + "?sessionId=" + measureVal + "&htdId=" + segVal, "xls");
        }
        else {
            //            objWebOffice.LoadOriginalFile("", "xls");
        }

        //        objWebOffice.SetKeyCtrl(595, -1, 0); //屏蔽 保存快捷键(Ctrl+S) 
        //        objWebOffice.SetKeyCtrl(592, -1, 0); //屏蔽 打印快捷键(Ctrl+P) 
        //        objWebOffice.HideMenuItem(0x01 + 0x02 + 0x04 + 0x20 + 0x1000 + 0x4000); //隐藏菜单
        //        objWebOffice.HideMenuArea('hideall', '', '', '');
        //        objWebOffice.ProtectDoc(1, 2, "KingRoad@)!Center2016"); //设置文档保护
    }
    //
    //判断是否安装webOffice组件
    //
    function _checkWebOffice(id) {
        $("#progressBar,#background").hide();
        try {
            if (!document.getElementById(id).LoadOriginalFile) {
                return false;
            } else
                return true;
        } catch (e) {
            return true;
        }

    }
};
